package org.tote.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tote.dao.DaoFacade;
import org.tote.model.Audit;
import org.tote.model.AuditOperationType;
import org.tote.model.MailMessageCreator;
import org.tote.model.MailMessageTemplate;
import org.tote.model.MailMessageType;
import org.tote.model.User;
import org.tote.model.UserLight;

@Service
@Transactional
public class MailBusinessServiceImpl implements MailBusinessService {

	private static final Logger LOGGER = LoggerFactory.getLogger(MailBusinessServiceImpl.class);

	@Autowired
	private JavaMailSender mailSender;

	@Value("#{application.url}")
	private String applicationUrl;

	@Value("#{application.email}")
	private String MAILER_NAME;

	@Autowired
	private DaoFacade daoFacade;

	@Autowired
	AuditService auditService;

	@Async
	@Override
	public void sendPostRegistrationMail(User user, UserLight sender) {
		try {
			MailMessageTemplate messageTemplate = daoFacade
					.getMailMessageTemplateById(MailMessageType.POST_REGISTRATION);
			messageTemplate
					.addParameter("username", user.getName())
					.addParameter("activationLink", applicationUrl + "activate/" + user.getToken())
					.addParameter("password", user.getPassword());
			MailMessageCreator messageHelper = new MailMessageCreator(
					mailSender.createMimeMessage(), messageTemplate, MAILER_NAME);
			messageHelper.setTo(user.getEmail());
			mailSender.send(messageHelper.getMimeMessage());
			String msg = "Post registration message was sent to " + user.getUsername() + " to "
					+ user.getEmail();
			auditService.saveAudit(new Audit(AuditOperationType.SEND, msg, sender));
			LOGGER.info(msg);
			//} catch (MessagingException | MailException e) {	
		} catch (Throwable e) {
			reportMailingFailure(e);
		}
	}

	@Async
	@Override
	public void sendRestorePasswordMail(User user, String newPassword, UserLight sender) {
		try {
			MailMessageTemplate messageTemplate = daoFacade
					.getMailMessageTemplateById(MailMessageType.RESTORE_PASSWORD);
			messageTemplate.addParameter("username", user.getName())
					.addParameter("loginLink", applicationUrl + "login")
					.addParameter("password", newPassword);
			MailMessageCreator messageCreator = new MailMessageCreator(
					mailSender.createMimeMessage(), messageTemplate, MAILER_NAME);
			messageCreator.setTo(user.getEmail());
			mailSender.send(messageCreator.getMimeMessage());
			String msg = "Restore password message was sent to " + user.getUsername() + " to "
					+ user.getEmail();
			auditService.saveAudit(new Audit(AuditOperationType.SEND, msg, sender));
			LOGGER.info(msg);
			//} catch (MessagingException | MailException e) {
		} catch (Throwable e) {
			reportMailingFailure(e);
		}
	}

	private void reportMailingFailure(Throwable e) {
		LOGGER.error("Unable to send email.", e);
		throw new RuntimeException("Unable to send email.", e);
	}
}
